package com.supermap.dataservice.cloud.cust.user;

import com.supermap.dubbo.api.cloud.cust.role.TreeNodeList;
import org.apache.ibatis.annotations.*;
import org.apache.logging.log4j.util.Strings;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @desc
 * @date 2019年03月04 15:07
 * @email 1301584033@qq.com
 */
@Mapper
public interface SysUserMapper {


    //获取用户菜单
    @SelectProvider(type = SysUserMapperSQL.class,method = "getUserTreeAreaRoleSQL")
    List<Map<String,Object>> getUserTreeAreaRole(@Param("areaCode")String areaCode, @Param("areaRoleId")Integer areaRoleId,@Param("loginName")String loginName,@Param("limit")Integer limit);


    @Select("select group_concat(concat(\n" +
            "a,',',\n" +
            "b,',',\n" +
            "c,',',\n" +
            "d,',',\n" +
            "e,',',\n" +
            "f\n" +
            ")) from (select \n" +
            "(select id from sys_menu where code=left(t1.code,3) ) as a, \n" +
            "(select id from sys_menu where code=left(t1.code,6) ) as b, \n" +
            "(select id from sys_menu where code=left(t1.code,9) ) as c, \n" +
            "(select id from sys_menu where code=left(t1.code,12) ) as d, \n" +
            "(select id from sys_menu where code=left(t1.code,15) ) as e, \n" +
            "(select id from sys_menu where code=left(t1.code,18) ) as f\n" +
            " from sys_menu t1 where t1.id in(${menuIds}))t1")
    String getParntysMenuId(@Param("menuIds")String menuIds);
    @Select("select menuid from sys_user_order_menu where userid=#{userId}")
    List<Integer> getSysMenuId(@Param("userId")Integer userId);
    @Select("select t1.id from sys_menu t1\n" +
            "inner join sys_area_menu t2 \n" +
            "on t1.id=t2.menuid and t1.state=0\n" +
            "inner join sys_area_role t3\n" +
            "on t2.areacode=t3.areacode\n" +
            "and t3.id=#{roleId}  and t3.areacode=#{areaCode} ")
    List<Integer> getAreaRole(@Param("roleId")Integer roleId,@Param("areaCode")String areaCode);


    @InsertProvider(type = SysUserMapperSQL.class,method = "putMenuISQL")
    Integer putMenuI(@Param("list") List<Integer> list,@Param("userId")Integer userId);


    @Insert("Insert  ignore into sys_user_role(userid,roleid) values(#{userId},#{role})")
    Integer putUserAreaRole(@Param("userId")Integer userId,@Param("role")Integer role);

    @Delete("delete from sys_user_role where userid=#{userId}")
    Integer delUserAreaRole(@Param("userId")Integer userId);

    @Delete("delete from sys_user_menu where userid=#{userId}")
    Integer delUserMenu(@Param("userId")Integer userId);

    @Select("select t2.id,t1.name,(select count(1) from sys_user_role where  userid=#{userId} and roleid=t2.id) as state from t_role t1\n" +
            "                    inner join sys_area_role t2\n" +
            "                    on t1.id=t2.roleid\n" +
            "                    inner join sys_area_role_menu t3\n" +
            "                    on t2.id=t3.arearoleid  where 1=1 and t1.del_flg=0  and t2.areacode=#{areaCode} ")
    List<TreeNodeList> getAllAreaRoleByRoleId(@Param("areaCode")String  areaCode,@Param("userId")Integer  userId);

    class  SysUserMapperSQL{

        public String getUserTreeAreaRoleSQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            String areaCode=(String)map.get("areaCode");
            Integer areaRoleId=(Integer)map.get("areaRoleId");
            String loginName=(String)map.get("loginName");
            Integer limit=(Integer)map.get("limit");
            stringBuilder.append("select t1.*,t2.roleid from (select id,username from t_user where del_flg=0 and areacode like concat('"+areaCode+"','%')  ");
            if(Strings.isNotBlank(loginName)){
                stringBuilder.append(" and loginname='"+loginName+"' ");
            }
            stringBuilder.append( " ) t1 inner join t_user_role t2\n" +
                    "on t1.id=t2.userid\n" +
                    "inner join sys_area_role t3\n" +
                    "on t2.roleid=t3.roleid and t3.areacode like concat('"+areaCode+"','%') and t3.id="+areaRoleId+"  limit "+limit+" ");

            return stringBuilder.toString();
        }

        public String  putMenuISQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            List<Integer> list=(List<Integer>)map.get("list");
            Integer userId=(Integer)map.get("userId");
            stringBuilder.append("insert into sys_user_menu(menuid,userid) values ");
            for (Integer menu:list){
                stringBuilder.append("("+menu+","+userId+"),");
            }

            return stringBuilder.toString().substring(0,stringBuilder.length()-1);
        }
    }
}
